package fl.ass.service.api;

import cmm.comm.facade.upload.api.IUploadApiService;
import cmm.comm.facade.upload.condition.SysUlFilesRecQueryCondition;
import cmm.comm.facade.upload.dto.SysUlFilesRecDTO;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.utils.*;
import cn.fl.project.constant.PrjPrdDtlConstant;
import cn.fl.project.dto.PrjBscInfoDTO;
import cn.fl.project.service.IProjectApiService;
import com.alibaba.fastjson.JSON;
import fl.ass.facade.api.IAssTrailCarApi;
import fl.ass.facade.api.IConvertibleCarFlowFactoryApi;
import fl.ass.facade.condition.AssDealCarCostQueryCondition;
import fl.ass.facade.condition.AssTrailCarDInfQueryCondition;
import fl.ass.facade.constant.AssFileConstant;
import fl.ass.facade.constant.DealCarFlowEnum;
import fl.ass.facade.constant.InterfaceConstants;
import fl.ass.facade.dto.*;
import fl.ass.service.dao.AssTrailCarDMapper;
import fl.ass.service.service.*;
import fl.flow.facade.flow.constant.FlowConstant;
import fl.flow.facade.flow.dto.FlowCompleteParamsDTO;
import fl.flow.facade.flow.service.impl.FlowBusnessServiceImpl;
import fl.flow.service.flow.service.ISysFlowSHService;
import fl.inf.facade.api.IWarrantApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

 /**
  * @Author huangkailun
  * @Date 09:38 2020-02-28
  * @Description 转优车流程回调接口
  */
@Service
public class ConvertibleCarFlowFactoryApi extends FlowBusnessServiceImpl implements IConvertibleCarFlowFactoryApi {

	/**
	 * 流程启动历史服务
	 */
	@Autowired
	private ISysFlowSHService sysFlowSHService;
	/**
	 * 处置-转优车信息表 服务层接口
	 */
	@Autowired
	private IAssDealCarService assDealCarService;
	/**
	 * 拖车服务
	 */
	@Autowired
	private IAssTrailCarApi assTrailCarApi;
	/**
	 * 拖车明细服务
	 */
	@Autowired
	private IAssTrailCarDService assTrailCarDService;
	/**
	 * 权证服务
	 */
	@Autowired
	private IWarrantApiService warrantApiService;
	@Autowired
	private AssTrailCarDMapper assTrailCarDMapper;

    /**
     * 拖车实际明细
     */
	@Autowired
    private IAssTrailCarDInfService assTrailCarDInfService;

    @Autowired
    private IAssParkingMgService assParkingMgService;

    @Autowired
    private IAssDealCarCostService assDealCarCostService;

    @Autowired
    private IUploadApiService uploadApiService;

    @Autowired
    private IProjectApiService projectApiService;

    @Autowired
    private IAssTrailCarMService assTrailCarMService;

	@Override
	public Map<String, Object> executeBefore(FlowCompleteParamsDTO flowCompleteParamsDTO) throws BaseException {
        Map<String, Object> params = new HashMap<String, Object>();
        // 流程KEY
        String procDefKey = flowCompleteParamsDTO.getProcDefKey();
        // 任务KEY
        String taskDefKey = flowCompleteParamsDTO.getTaskDefKey();
        // 分支KEY
        String branchKey = flowCompleteParamsDTO.getBranchKey();
        // 流程实例ID
        String procInstId = flowCompleteParamsDTO.getProcInstId();
        Long id = flowCompleteParamsDTO.getBusinessKey();
        if ("FLOW_0000_020_001".equals(procDefKey) && StringUtils.isNotEmpty(taskDefKey) && StringUtils.isNotEmpty(branchKey)) {
            AssDealCarDTO assDealCarDTO = this.assDealCarService.selectById(id);
            Long detailId = assDealCarDTO.getDetailId();
            AssTrailCarDDTO assTrailCarDDTO = assTrailCarDService.selectById(detailId);
            MessageDTO<String> result = null;
            switch (branchKey) {
                case "FLOW_0000_020_001_001_001":
                    // 通过（过户资料上传）
                    Integer assignFlag = assDealCarDTO.getIsAssign();
                    // 提户情况
                    Integer assAccTyp = assDealCarDTO.getAssAccTyp();
                    if (StringUtils.isEmpty(assignFlag)) {
                        throw new BaseException("请先保存是否可过户信息.");
                    } else if (BaseConstant.IS_YESNO_NO == assignFlag) {
                        // 是否可过户：否
                        if (StringUtils.isEmpty(assDealCarDTO.getIsDiffPlace())) {
                            throw new BaseException("请先保存是否异地提档信息.");
                        }
                    } else if (BaseConstant.IS_YESNO_YES == assignFlag) {
                        // 公共校验方法
                        validateCommon(assDealCarDTO, assAccTyp, id);
                        // 校验文件上传
                        uploadFileValidate(id, assAccTyp);
                    }
                    params.put("pass", BaseConstant.IS_YESNO_YES);
                    break;
                case "FLOW_0000_020_001_003_001" :
                    // 提交（过户资料上传->省总大区总审批驳回后）
                    assignFlag = assDealCarDTO.getIsAssign();
                    // 提户情况
                    assAccTyp = assDealCarDTO.getAssAccTyp();
                    if (StringUtils.isEmpty(assignFlag)) {
                        throw new BaseException("请先保存是否可过户信息.");
                    } else if (BaseConstant.IS_YESNO_NO == assignFlag) {
                        // 是否可过户：否
                        if (StringUtils.isEmpty(assDealCarDTO.getIsDiffPlace())) {
                            throw new BaseException("请先保存是否异地提档信息.");
                        }
                    } else if (BaseConstant.IS_YESNO_YES == assignFlag) {
                        // 公共校验方法
                        validateCommon(assDealCarDTO, assAccTyp, id);
                        // 校验文件上传
                        uploadFileValidate(id, assAccTyp);
                    }
                    params.put("pass", BaseConstant.IS_YESNO_YES);
                    break;
                case "FLOW_0000_020_001_002_001":
                    // 通过（省总/大区总审批）
                    assignFlag = assDealCarDTO.getIsAssign();
                    params.put("assignFlag", assignFlag);
                    // 提户情况
                    assAccTyp = assDealCarDTO.getAssAccTyp();
                    if (StringUtils.isEmpty(assignFlag)) {
                        throw new BaseException("请先保存是否可过户信息.");
                    } else if (BaseConstant.IS_YESNO_NO == assignFlag) {
                        // 无户转优车（省总/大区总审批）
                        modifyDealCar(assDealCarDTO, detailId, result, assignFlag);
                        // 无户转优车修改支付表状态
                        updatePayStatus(assTrailCarDDTO, procDefKey, procInstId, result);
                        // 同步修改疫情支付表状态
                        ProductStatusLogDTO productStatusLogDTO = new ProductStatusLogDTO();
                        productStatusLogDTO.setPaySchId(assTrailCarDDTO.getPaySchId());
                        productStatusLogDTO.setPrjPrdId(assTrailCarDDTO.getPrjPrdDtlId());
                        productStatusLogDTO.setCurrentStatus(PrjPrdDtlConstant.CODE_PRJ_PRD_STS_CD_20);
                        productStatusLogDTO.setOpCode(InterfaceConstants.OP_CODE_OP0029);
                        productStatusLogDTO.setProcessInstanceCode(flowCompleteParamsDTO.getProcDefKey());
                        productStatusLogDTO.setProcessInstanceId(flowCompleteParamsDTO.getProcInstId());
                        this.assTrailCarMService.updateYQPrdStsCdByFlow(productStatusLogDTO);
                    } else if (BaseConstant.IS_YESNO_YES == assignFlag) {
                        // 公共校验方法
                        validateCommon(assDealCarDTO, assAccTyp, id);
                        // 校验文件上传
                        uploadFileValidate(id, assAccTyp);
                    }
                    break;
                case "FLOW_0000_020_001_002_002":
                    // 驳回（省总/大区总审批）
                    // 设置节点处理人为客户经理
                    setAssigneeCstMgr(detailId, params);
                    break;
                case "FLOW_0000_020_001_003_V2_001":
                    // 提交（过户资料上传至优车事业部审批指定过户人）
                    params.put("pass", BaseConstant.IS_YESNO_YES);
                    break;
                case "FLOW_0000_020_001_004_001":
                    // 通过（优车事业部审批指定过户人）
                    assignFlag = assDealCarDTO.getIsAssign();
                    if (BaseConstant.IS_YESNO_NO == assignFlag) {
                        throw new BaseException("是否可过户为否，不能通过流程");
                    }
                    // 设置节点处理人为客户经理
                    setAssigneeCstMgr(detailId, params);
                    // 校验资产管理员是否必填
                    if (StringUtils.isEmpty(assDealCarDTO.getManageId())) {
                        throw new BaseException("请选择资产管理员");
                    }
                    break;
                case "FLOW_0000_020_001_004_002":
                    // 无户转优车（优车事业部审批指定过户人）
                    modifyDealCar(assDealCarDTO, detailId, result, BaseConstant.IS_YESNO_NO);
                    // 无户转优车修改支付表状态
                    updatePayStatus(assTrailCarDDTO, procDefKey, procInstId, result);
                    // 同步修改疫情支付表状态
                    ProductStatusLogDTO productStatusLogDTO = new ProductStatusLogDTO();
                    productStatusLogDTO.setPaySchId(assTrailCarDDTO.getPaySchId());
                    productStatusLogDTO.setPrjPrdId(assTrailCarDDTO.getPrjPrdDtlId());
                    productStatusLogDTO.setCurrentStatus(PrjPrdDtlConstant.CODE_PRJ_PRD_STS_CD_20);
                    productStatusLogDTO.setOpCode(InterfaceConstants.OP_CODE_OP0029);
                    productStatusLogDTO.setProcessInstanceCode(flowCompleteParamsDTO.getProcDefKey());
                    productStatusLogDTO.setProcessInstanceId(flowCompleteParamsDTO.getProcInstId());
                    this.assTrailCarMService.updateYQPrdStsCdByFlow(productStatusLogDTO);
                    break;
                case "FLOW_0000_020_001_004_003":
                    // 驳回（优车事业部审批指定过户人）
                    // 设置节点处理人为客户经理
                    setAssigneeCstMgr(detailId, params);
                    break;
                case "FLOW_0000_020_001_005_001":
                    // 通过（客户经理添加邮寄信息）
                    if (StringUtils.isEmpty(assDealCarDTO.getRecipientName())) {
                        throw new BaseException("请填写收件人（姓名）！");
                    }
                    if (StringUtils.isEmpty(assDealCarDTO.getReceiveType())) {
                        throw new BaseException("请选择接收方式！");
                    } else if (BaseConstant.IS_YESNO_YES_STR.equals(assDealCarDTO.getReceiveType())) {
                        if (StringUtils.isEmpty(assDealCarDTO.getExpress())) {
                            throw new BaseException("请填写快递公司！");
                        }
                        if (StringUtils.isEmpty(assDealCarDTO.getExpressNumber())) {
                            throw new BaseException("请填写快递单号！");
                        }
                    }
                    if (StringUtils.isEmpty(assDealCarDTO.getContactNumber())) {
                        throw new BaseException("请填写联系电话！");
                    }
                    params.put("pass", BaseConstant.IS_YESNO_YES);
                    // 设置处理人为资产管理员
                    if (StringUtils.isNotEmpty(assDealCarDTO.getManageId())) {
                        setAssetsMgr(Long.valueOf(assDealCarDTO.getManageId()), params);
                    }
                    break;
                case "FLOW_0000_020_001_006_001":
                    // 通过（资产管理员/过户人办理过户）
                    // 添加坏账拨备
                    calcBadDebts(assDealCarDTO.getDetailId());
                    break;
                case "FLOW_0000_020_001_007_001":
                    // 通过（优车系统管理员审核是否完成过户）
                    assignFlag = assDealCarDTO.getIsAssign();
                    params.put("assignFlag", assignFlag);
                    if (BaseConstant.IS_YESNO_YES == assignFlag) {
                        // 转优车（省总/大区总审批）
                        modifyDealCar(assDealCarDTO, detailId, result, assignFlag);
                        // 转优车修改支付表状态
                        updatePayStatus(assTrailCarDDTO, procDefKey, procInstId, result);
                        // 同步修改疫情支付表状态
                        productStatusLogDTO = new ProductStatusLogDTO();
                        productStatusLogDTO.setPaySchId(assTrailCarDDTO.getPaySchId());
                        productStatusLogDTO.setPrjPrdId(assTrailCarDDTO.getPrjPrdDtlId());
                        productStatusLogDTO.setCurrentStatus(PrjPrdDtlConstant.CODE_PRJ_PRD_STS_CD_20);
                        productStatusLogDTO.setOpCode(InterfaceConstants.OP_CODE_OP0029);
                        productStatusLogDTO.setProcessInstanceCode(flowCompleteParamsDTO.getProcDefKey());
                        productStatusLogDTO.setProcessInstanceId(flowCompleteParamsDTO.getProcInstId());
                        this.assTrailCarMService.updateYQPrdStsCdByFlow(productStatusLogDTO);

                    }
                    break;
                case "FLOW_0000_020_001_007_003":
                    // 驳回（优车系统管理员审核是否完成过户）
                    // 设置处理人为资产管理员
                    if (StringUtils.isNotEmpty(assDealCarDTO.getManageId())) {
                        setAssetsMgr(Long.valueOf(assDealCarDTO.getManageId()), params);
                    }
                    break;
                case "FLOW_0000_020_001_008_001":
                    // 通过（优车资产经理审批是否完成过户）
                    // 转优车（优车资产经理审批是否完成过户）
                    modifyDealCar(assDealCarDTO, detailId, result, assDealCarDTO.getIsAssign());
                    // 转优车修改支付表状态
                    updatePayStatus(assTrailCarDDTO, procDefKey, procInstId, result);
                    // 同步修改疫情支付表状态
                    productStatusLogDTO = new ProductStatusLogDTO();
                    productStatusLogDTO.setPaySchId(assTrailCarDDTO.getPaySchId());
                    productStatusLogDTO.setPrjPrdId(assTrailCarDDTO.getPrjPrdDtlId());
                    productStatusLogDTO.setCurrentStatus(PrjPrdDtlConstant.CODE_PRJ_PRD_STS_CD_20);
                    productStatusLogDTO.setOpCode(InterfaceConstants.OP_CODE_OP0029);
                    productStatusLogDTO.setProcessInstanceCode(flowCompleteParamsDTO.getProcDefKey());
                    productStatusLogDTO.setProcessInstanceId(flowCompleteParamsDTO.getProcInstId());
                    this.assTrailCarMService.updateYQPrdStsCdByFlow(productStatusLogDTO);
                    break;
                case "FLOW_0000_020_001_008_002":
                    // 驳回（优车资产经理审批是否完成过户）
                    // 设置处理人为资产管理员
                    if (StringUtils.isNotEmpty(assDealCarDTO.getManageId())) {
                        setAssetsMgr(Long.valueOf(assDealCarDTO.getManageId()), params);
                    }
                    break;
            }
            // 设置下一节点的key保存在转优车表中
            this.setTaskDefKey(branchKey, assDealCarDTO);
        }
        return params;
    }

    @Override
    public void executeAfter(FlowCompleteParamsDTO flowCompleteParamsDTO) throws BaseException {
        String branchKey = flowCompleteParamsDTO.getBranchKey();//分支KEY
        Long id = flowCompleteParamsDTO.getBusinessKey();
        AssDealCarDTO dto = assDealCarService.selectById(id);
        // 是否可过户
        Integer assignFlag = dto.getIsAssign();
        switch (branchKey) {
            case "FLOW_0000_020_001_002_001":
                if (BaseConstant.IS_YESNO_NO == assignFlag) {
                    // 无户转优车（省总/大区总审批）
                    this.pass(flowCompleteParamsDTO);
                }
                break;
            case "FLOW_0000_020_001_004_002":
                // 无户转优车（优车事业部审批指定过户人）
                this.pass(flowCompleteParamsDTO);
                break;
            case "FLOW_0000_020_001_007_001":
                if (BaseConstant.IS_YESNO_YES == assignFlag) {
                    // 已完成过户（优车系统管理员审核是否完成过户）
                    this.pass(flowCompleteParamsDTO);
                }
                break;
            case "FLOW_0000_020_001_008_001":
                // 无户转优车（优车资产经理审批是否完成过户）
                this.pass(flowCompleteParamsDTO);
                break;
            default:
                break;
        }
    }

     /**
      * 根据分支key设置流程所在节点
      * @param branchKey
      * @param dto
      */
    private void setTaskDefKey(String branchKey, AssDealCarDTO dto) {
        try {
            switch (branchKey) {
                case "FLOW_0000_020_001_001_001":
                    // 通过（过户资料上传）
                    dto.setTaskDefKey(DealCarFlowEnum.FLOW_0002.getTaskDefKey());
                    dto.setTaskDefKeyNm(DealCarFlowEnum.FLOW_0002.getTaskDefKeyNm());
                    break;
                case "FLOW_0000_020_001_002_001":
                    // 通过（省总/大区总审批）
                    Integer assignFlag = dto.getIsAssign();
                    if (BaseConstant.IS_YESNO_YES == assignFlag) {
                        dto.setTaskDefKey(DealCarFlowEnum.FLOW_0004.getTaskDefKey());
                        dto.setTaskDefKeyNm(DealCarFlowEnum.FLOW_0004.getTaskDefKeyNm());
                    }
                    break;
                case "FLOW_0000_020_001_002_002":
                    // 驳回（省总/大区总审批）
                    dto.setTaskDefKey(DealCarFlowEnum.FLOW_0009.getTaskDefKey());
                    dto.setTaskDefKeyNm(DealCarFlowEnum.FLOW_0009.getTaskDefKeyNm());
                    break;
                case "FLOW_0000_020_001_003_001":
                    // 提交（过户资料上传至省总/大区总审批）
                    dto.setTaskDefKey(DealCarFlowEnum.FLOW_0002.getTaskDefKey());
                    dto.setTaskDefKeyNm(DealCarFlowEnum.FLOW_0002.getTaskDefKeyNm());
                    break;
                case "FLOW_0000_020_001_004_001":
                    // 通过（优车事业部审批指定过户人）
                    dto.setTaskDefKey(DealCarFlowEnum.FLOW_0005.getTaskDefKey());
                    dto.setTaskDefKeyNm(DealCarFlowEnum.FLOW_0005.getTaskDefKeyNm());
                    break;
                case "FLOW_0000_020_001_004_002":
                    // 资料不全无法过户（优车事业部审批指定过户人）
                    break;
                case "FLOW_0000_020_001_004_003":
                    // 驳回（优车事业部审批指定过户人）
                    dto.setTaskDefKey(DealCarFlowEnum.FLOW_0010.getTaskDefKey());
                    dto.setTaskDefKeyNm(DealCarFlowEnum.FLOW_0010.getTaskDefKeyNm());
                    break;
                case "FLOW_0000_020_001_003_V2_001":
                    // 提交（过户资料上传至优车事业部审批指定过户人）
                    dto.setTaskDefKey(DealCarFlowEnum.FLOW_0004.getTaskDefKey());
                    dto.setTaskDefKeyNm(DealCarFlowEnum.FLOW_0004.getTaskDefKeyNm());
                    break;
                case "FLOW_0000_020_001_005_001":
                    // 通过（客户经理添加邮寄信息）
                    dto.setTaskDefKey(DealCarFlowEnum.FLOW_0006.getTaskDefKey());
                    dto.setTaskDefKeyNm(DealCarFlowEnum.FLOW_0006.getTaskDefKeyNm());
                    break;
                case "FLOW_0000_020_001_006_001":
                    // 通过（资产管理员/过户人办理过户）
                    dto.setTaskDefKey(DealCarFlowEnum.FLOW_0007.getTaskDefKey());
                    dto.setTaskDefKeyNm(DealCarFlowEnum.FLOW_0007.getTaskDefKeyNm());
                    break;
                case "FLOW_0000_020_001_007_001":
                    // 通过（优车系统管理员审核是否完成过户）
                    assignFlag = dto.getIsAssign();
                    if (BaseConstant.IS_YESNO_YES == assignFlag) {
                        dto.setTaskDefKey("");
                        dto.setTaskDefKeyNm("");
                    } else if (BaseConstant.IS_YESNO_NO == assignFlag) {
                        dto.setTaskDefKey(DealCarFlowEnum.FLOW_0008.getTaskDefKey());
                        dto.setTaskDefKeyNm(DealCarFlowEnum.FLOW_0008.getTaskDefKeyNm());
                    }
                    break;
                case "FLOW_0000_020_001_007_003":
                    // 驳回（优车系统管理员审核是否完成过户）
                    dto.setTaskDefKey(DealCarFlowEnum.FLOW_0006.getTaskDefKey());
                    dto.setTaskDefKeyNm(DealCarFlowEnum.FLOW_0006.getTaskDefKeyNm());
                    break;
                case "FLOW_0000_020_001_008_001":
                    // 通过（优车资产经理审批是否完成过户）
                    dto.setTaskDefKey("");
                    dto.setTaskDefKeyNm("");
                    break;
                case "FLOW_0000_020_001_008_002":
                    // 驳回（优车资产经理审批是否完成过户）
                    dto.setTaskDefKey(DealCarFlowEnum.FLOW_0006.getTaskDefKey());
                    dto.setTaskDefKeyNm(DealCarFlowEnum.FLOW_0006.getTaskDefKeyNm());
                    break;

            }
            assDealCarService.modify(dto);
        } catch (BaseException e) {
            BaseLogUtils.newLogger("更新转优车流程节点失败").setKeys("assDealCar:" + JSON.toJSONString(dto), "branchKey:" + branchKey).setThrowable(e).setWarning(false).error(LogExceptionEnum.COMMON_CODE_0002);
            throw e;
        } catch (Exception e) {
            BaseLogUtils.newLogger("更新转优车流程节点失败").setKeys("assDealCar:" + JSON.toJSONString(dto), "branchKey:" + branchKey).setThrowable(e).setWarning(false).error(LogExceptionEnum.COMMON_CODE_0002);
            throw e;
        }
    }

    /**
     * 设置节点处理人为资产管理员
     */
    private void setAssetsMgr(Long ManageId, Map<String, Object> params) {
        // 设置处理人为客户经理
        String[] str = {String.valueOf(ManageId)};
        params.put("assetsMgr", str);
    }

     /**
      * 设置节点处理人为客户经理
      */
    private void setAssigneeCstMgr(Long detailId, Map<String, Object> params) {
        try {
            // 设置处理人为客户经理
            AssTrailCarDDTO assTrailCarDDTO = assTrailCarDService.selectById(detailId);
            Long prjId = assTrailCarDDTO.getPrjId();
            if (StringUtils.isNotEmpty(prjId)) {
                PrjBscInfoDTO dto = this.projectApiService.selectById(prjId);
                Long cstMgrId = dto.getCstMgrId();
                String[] str = {String.valueOf(cstMgrId)};
                params.put("cstMgrAssignee", str);
            }
        } catch (BaseException e) {
            BaseLogUtils.newLogger("设置下一节点处理人为客户经理失败").setKeys("detailId:" + detailId).setThrowable(e).setWarning(false).error(LogExceptionEnum.COMMON_CODE_0002);
            throw e;
        } catch (Exception e) {
            BaseLogUtils.newLogger("设置下一节点处理人为客户经理失败").setKeys("detailId:" + detailId).setThrowable(e).setWarning(false).error(LogExceptionEnum.COMMON_CODE_0002);
            throw e;
        }
    }

    /**
     * 文件上传校验
     */
    private void uploadFileValidate(Long dealId, Integer assAccTyp) {
        try {
            SysUlFilesRecQueryCondition sysUlFilesRecQueryCondition = new SysUlFilesRecQueryCondition();
            sysUlFilesRecQueryCondition.setPkId(dealId.toString());
            sysUlFilesRecQueryCondition.setIsDel(BaseConstant.IS_YESNO_NO);
            sysUlFilesRecQueryCondition.setCfgCd(AssFileConstant.ASS_PRO_FILE_CFG_CD_006);
            sysUlFilesRecQueryCondition.setSysCd(AssFileConstant.ASS_PRO_SYS_CD);
            List<String> cfgCdDtlList = new ArrayList<>(4);
            List<SysUlFilesRecDTO> sysUlFilesRecDTOList = uploadApiService.searchSysUlFilesRecList(sysUlFilesRecQueryCondition);
            sysUlFilesRecDTOList.stream().forEach(sysUlFilesRecDTO -> {
                cfgCdDtlList.add(sysUlFilesRecDTO.getCfgDtlCd());
            });
            BaseLogUtils.newLogger("打印文件上传数据").setKeys("sysUlFilesRecDTOList:" + JSON.toJSONString(sysUlFilesRecDTOList), "cfgCdDtlList:" + JSON.toJSONString(cfgCdDtlList), "assAccTyp:" + assAccTyp).info();
            if (BaseConstant.IS_YESNO_YES == assAccTyp) {
                // 提户情况：公户
                if (!cfgCdDtlList.contains(AssFileConstant.ASS_PRO_CFGDTLCD_NEW_10) || !cfgCdDtlList.contains(AssFileConstant.ASS_PRO_CFGDTLCD_NEW_20)
                        || !cfgCdDtlList.contains(AssFileConstant.ASS_PRO_CFGDTLCD_NEW_30) || !cfgCdDtlList.contains(AssFileConstant.ASS_PRO_CFGDTLCD_NEW_40)) {
                    throw new BaseException("请完整上传公户对应资料！");
                }
            } else if (BaseConstant.IS_YESNO_NO == assAccTyp) {
                // 提户情况：私户
                if (!cfgCdDtlList.contains(AssFileConstant.ASS_PRO_CFGDTLCD_NEW_10) || !cfgCdDtlList.contains(AssFileConstant.ASS_PRO_CFGDTLCD_NEW_20)
                        || !cfgCdDtlList.contains(AssFileConstant.ASS_PRO_CFGDTLCD_NEW_50) || !cfgCdDtlList.contains(AssFileConstant.ASS_PRO_CFGDTLCD_NEW_60)) {
                    throw new BaseException("请完整上传私户对应资料！");
                }
            }
        } catch (BaseException e) {
            BaseLogUtils.newLogger("文件上传校验失败").setKeys("dealId:" + dealId).setThrowable(e).setWarning(false).error(LogExceptionEnum.COMMON_CODE_0002);
            throw e;
        } catch (Exception e) {
            BaseLogUtils.newLogger("文件上传校验失败").setKeys("dealId:" + dealId).setThrowable(e).setWarning(false).error(LogExceptionEnum.COMMON_CODE_0002);
            throw e;
        }
    }

     /**
      * 公共校验提取方法
      * @param assDealCarDTO
      * @param assAccTyp
      * @param id
      */
    private void validateCommon(AssDealCarDTO assDealCarDTO, Integer assAccTyp, Long id) {
        // 是否可过户：是
        if (StringUtils.isEmpty(assDealCarDTO.getIsDiffPlace())) {
            throw new BaseException("请先保存是否异地提档信息.");
        }
        if (StringUtils.isEmpty(assAccTyp)) {
            throw new BaseException("请先保存提户情况信息.");
        }
        AssDealCarCostQueryCondition assDealCarCostQueryCondition = new AssDealCarCostQueryCondition();
        assDealCarCostQueryCondition.setCarId(id);
        // 过户费查询
        assDealCarCostQueryCondition.setCostTypeCd("1");
        assDealCarCostQueryCondition.setIsDel(BaseConstant.IS_YESNO_NO);
        List<AssDealCarCostDTO> assDealCarCostDTOS = assDealCarCostService.searchList(assDealCarCostQueryCondition);
        if (CollectionUtils.isEmpty(assDealCarCostDTOS) || StringUtils.isEmpty(assDealCarCostDTOS.get(0).getCostAmt())) {
            throw new BaseException("请先保存过户费信息.");
        }
    }

    /**
     * 无户转优车
     */
    private void modifyDealCar(AssDealCarDTO assDealCarDTO, Long detailId, MessageDTO<String> result, Integer isAssign) {
        try {
            // 设置转优车时间
            Date d = DateUtils.getCurrentDate(DateUtils.YYYY_MM_DD);
            assDealCarDTO.setIsAssign(isAssign);
            assDealCarDTO.setDealTm(d);
            assDealCarDTO.setFlowStatusCd(InterfaceConstants.FLOW_STATUS_CD_3);
            boolean success = this.assDealCarService.modify(assDealCarDTO);
            if (!success) {
                throw new BaseException("更新转优车时间失败.");
            }
            AssTrailCarDDTO assTrailCarDDTO = assTrailCarDService.selectById(detailId);
            // 处置状态：已转优车
            assTrailCarDDTO.setDealStsCd(InterfaceConstants.DEAL_STATUS_CD_11);
            result = this.assTrailCarApi.updateAssTrailCarDetailInfo(assTrailCarDDTO);
            if (!result.isSuccess()) {
                throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_603, "更新拖车明细表处置状态失败.");
            }
        } catch (BaseException e) {
            BaseLogUtils.newLogger("无户转优车失败").setKeys("assDealCarDTO:" + JSON.toJSONString(assDealCarDTO)).setThrowable(e).setWarning(false).error(LogExceptionEnum.COMMON_CODE_0002);
            throw e;
        } catch (Exception e) {
            BaseLogUtils.newLogger("无户转优车失败").setKeys("assDealCarDTO:" + JSON.toJSONString(assDealCarDTO)).setThrowable(e).setWarning(false).error(LogExceptionEnum.API_CODE_0102);
            throw e;
        }
    }

     /**
      * 计算坏账拨备
      */
    private void calcBadDebts(Long detailId) {
        try {
            AssTrailCarDInfQueryCondition qc = new AssTrailCarDInfQueryCondition();
            qc.setDetailId(detailId);
            List<AssTrailCarDInfDTO> list = assTrailCarDInfService.searchList(qc);
            if (CollectionUtils.isNotEmpty(list)) {
                AssTrailCarDInfDTO dto = list.get(0);
                if (StringUtils.isNotEmpty(dto.getBaseAmt())) {
                    Double baseAmt = DecimalUtils.div(dto.getBaseAmt(), 2, 0);
                    Long badDebts = Long.valueOf(baseAmt.longValue());
                    dto.setBadDebts(badDebts);
                    boolean succ4BadDebts = assTrailCarDInfService.modify(dto);
                    if (!succ4BadDebts) {
                        throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_603, "更新坏账拨备失败.");
                    }
                }
            }
        } catch (BaseException e) {
            BaseLogUtils.newLogger("计算坏账拨备失败").setKeys("detailId:" + detailId).setThrowable(e).setWarning(false).error(LogExceptionEnum.COMMON_CODE_0002);
            throw e;
        } catch (Exception e) {
            BaseLogUtils.newLogger("计算坏账拨备失败").setKeys("detailId:" + detailId).setThrowable(e).setWarning(false).error(LogExceptionEnum.COMMON_CODE_0002);
            throw e;
        }
    }

    /**
     * 修改支付表状态
     */
    private void updatePayStatus(AssTrailCarDDTO assTrailCarDDTO, String procDefKey, String procInstId, MessageDTO<String> result) {
        try {
            BaseLogUtils.newLogger("更新支付表状态状态start").setBussinessId("PrjPrdDtlId:" + assTrailCarDDTO.getPrjPrdDtlId()).info();
            List<ProductStatusLogDTO> proList = new ArrayList<ProductStatusLogDTO>();
            ProductStatusLogDTO proDTO = new ProductStatusLogDTO();
            proDTO.setCurrentStatus(InterfaceConstants.PAY_STATUS_CD_S0106020301);
            proDTO.setPrjPrdId(assTrailCarDDTO.getPrjPrdDtlId());
            proDTO.setOpCode(InterfaceConstants.OP_CODE_OP0029);
            proDTO.setProcessInstanceCode(procDefKey);
            proDTO.setProcessInstanceId(procInstId);
            proList.add(proDTO);
            result = this.assTrailCarApi.updatePayStatus(proList);
            if (!result.isSuccess()) {
                throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_603, "更新支付表信息失败.");
            }
            BaseLogUtils.newLogger("更新支付表状态状态end").setBussinessId("PrjPrdDtlId:" + assTrailCarDDTO.getPrjPrdDtlId()).info();
        } catch (BaseException e) {
            BaseLogUtils.newLogger("更新支付表状态状态失败").setKeys("assTrailCarDDTO:" + JSON.toJSONString(assTrailCarDDTO)).setThrowable(e).setWarning(false).error(LogExceptionEnum.COMMON_CODE_0002);
            throw e;
        } catch (Exception e) {
            BaseLogUtils.newLogger("更新支付表状态状态失败").setKeys("assTrailCarDDTO:" + JSON.toJSONString(assTrailCarDDTO)).setThrowable(e).setWarning(false).error(LogExceptionEnum.API_CODE_0102);
            throw e;
        }
    }

    /**
     * 获取流程节点处理人的回调接口
     * @param flowCompleteParamsDTO
     * @return
     * @throws BaseException
     */
	@Override
	public String getFlowBusinessActMgrOrgCd(FlowCompleteParamsDTO flowCompleteParamsDTO) throws BaseException {
		Long id = flowCompleteParamsDTO.getBusinessKey();
		AssDealCarDTO assDealCarDTO = this.assDealCarService.selectById(id);
		//拖车明细数据
		List<AssTrailCarDDTO> assTrailCarDDTOList = assTrailCarApi.findTrailCarDetailListById(assDealCarDTO.getMasterId()).getDatas();
		String cstMgrOrgCd = "";
		if (assTrailCarDDTOList != null && assTrailCarDDTOList.size() > 0) {
			Long prjId = assTrailCarDDTOList.get(0).getPrjId();
			BaseLogUtils.newLogger("流程回调实现类获取项目客户经理组织架构代码开始").setKeys("taskId:" + flowCompleteParamsDTO.getTaskId(), "prjId:" + prjId).info();
			if(null!=prjId){
				 cstMgrOrgCd = assTrailCarDMapper.selectCstMgrOrgCd(prjId);
				if (null == cstMgrOrgCd || cstMgrOrgCd.equals("")) {
					BaseLogUtils.newLogger("根据ID未获取到业务数据!").info();
					throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_601, "根据ID未获取到业务数据!");
				}
                BaseLogUtils.newLogger("流程回调实现类获取项目客户经理组织架构代码结束").setKeys("taskId:" + flowCompleteParamsDTO.getTaskId(), "prjId:" + prjId, "cstMgrOrgCd:" + cstMgrOrgCd).info();
				return cstMgrOrgCd;
			}
		}
		BaseLogUtils.newLogger("流程接口回调>>返回项目客户经理组织架构>>未找到!").info();
		return super.getFlowBusinessActMgrOrgCd(flowCompleteParamsDTO);
	}

	/**
	 * 流程结束:通过
	 * @param flowCompleteParamsDTO
	 * @throws BaseException
	 */
	private void pass(FlowCompleteParamsDTO flowCompleteParamsDTO)throws BaseException{
		this.sysFlowSHService.modifyAudStsCd(InterfaceConstants.ASS_FLOW_S_H_ASS, FlowConstant.FLOW_CHECK_STATE_PASS, new Date(), new Date(),
				flowCompleteParamsDTO.getCrtUsrId(), flowCompleteParamsDTO.getBusinessKey(), flowCompleteParamsDTO.getProcDefKey(),
				flowCompleteParamsDTO.getProcDefId(), flowCompleteParamsDTO.getSysCd(), flowCompleteParamsDTO.getProcInstId());
	}
	/**
	 * 流程结束:不通过
	 * @param flowCompleteParamsDTO
	 * @throws BaseException
	 */
	private void noPass(FlowCompleteParamsDTO flowCompleteParamsDTO)throws BaseException{
		this.sysFlowSHService.modifyAudStsCd(InterfaceConstants.ASS_FLOW_S_H_ASS, FlowConstant.FLOW_CHECK_STATE_NO_PASS, new Date(), new Date(),
				flowCompleteParamsDTO.getCrtUsrId(), flowCompleteParamsDTO.getBusinessKey(), flowCompleteParamsDTO.getProcDefKey(),
				flowCompleteParamsDTO.getProcDefId(), flowCompleteParamsDTO.getSysCd(), flowCompleteParamsDTO.getProcInstId());
	}

}
